<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<div style="width: 95%;margin: 26px auto;">
    <div id="table_deleteAdmin">
    </div>
</div>

<script type="text/html" id="deleteAdminUserOperationTpl">
    <div>
        <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="updateUserType('{{d.id}}',0)">取消管理员权限
        </button>
    </div>
</script>
<script type="text/html" id="realNameTpl">
    {{#  if(d.realName === null){ }}
    <p style="color: darkorange;">未实名</p>
    {{#  } else { }}
    <p>{{d.realName}}</p>
    {{#  } }}
</script>
<script>
    let deleteAdminTable = null;
    layui.use('table', function () {
        var table = layui.table;

        deleteAdminTable = table.render({
            elem: '#table_deleteAdmin'
            , height: 480
            , cellMinWidth: 100
            , url: 'http://localhost/campus/admin/user/getAllUser?isAdminUser=true'
            , page: {
                layout: ['count']
            } //开启分页
            , toolbar: true
            , defaultToolbar: [{
                title: '刷新'
                , layEvent: 'refresh'
                , icon: 'layui-icon-refresh-1'
            }, 'filter', 'exports', 'print',]
            , cols: [[ //表头
                {field: 'id', title: '管理员ID', width: '18%', align: 'center', sort: true, fixed: 'left'}
                , {field: 'username', title: '昵称', width: '17%', align: 'center'}
                , {
                    field: 'realName', title: '真实姓名', width: '15%', align: 'center'
                    , templet: "#realNameTpl"
                }
                , {field: 'stringRegisterTime', title: '注册时间', width: '17%', sort: true, align: 'center'}
                , {field: 'phone', title: '联系电话', width: '15%', align: 'center'}
                , {field: 'operation', title: '操作', width: '18%', align: 'center', templet: "#deleteAdminUserOperationTpl"}
            ]]
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.users //解析数据列表
                };
            }
        });

        table.on('toolbar()', function (obj) {
            switch (obj.event) {
                case 'refresh':
                    deleteAdminTable.reload();
                    break;
            }
        });

    });

    function updateUserType(userId, type) {
        layer.confirm("确定取消此用户的管理权限？", {
            icon: 3
            , title: "提示"
        }, function (index) {
            $.ajax({
                url: "http://localhost/campus/admin/user/updateAccountType"
                , type: "GET"
                , data: "userId=" + userId + "&type=" + type
                , success: function (data) {
                    let icon = data.success ? 1 : 2;
                    layer.msg(data.message, {
                        icon: icon
                        , time: 2000
                    });
                    deleteAdminTable.reload();
                }
            });
            layer.close(index);
        });
    }
</script>
</body>
</html>
